// packageA/pages/xiaoxi/xiaoxi.js
const app = getApp()
const { forEach } = require("jszip");
const zim = app.globalData.zim;
// 设置日志显示
zim.setLogConfig({
  logLevel: 'error'
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    ggid:'',
    list:[],
    gglist:'',
    xiaoxi:[],
    xiaoxi111:0,
    hongdian:false,
    // list1:[
    //   '不限','胜率高低','被邀次数','游戏等级'
    // ],
    list1:[
      '不限','胜率降序','被邀请次','游戏等级高','被拉黑次少','好评多','胜率高'
    ],
    type:-1,
    type1:-1,
    name:'',
    navlist1:[
      '不限','男','女'
    ],
    saixuan:false,
    navlist2: [{
      id: "1",
      title: "已收藏",
      xz_flag: false
    },
    {
      id: "2",
      title: "同城",
      xz_flag: false
    },
    {
      id: "3",
      title: "开黑过",
      xz_flag: false
    },
    {
      id: "4",
      title: "可开黑",
      xz_flag: false
    },
    {
      id: "5",
      title: "有主页",
      xz_flag: false
    }
    ],
    page_no:1,
    tanchuang:false,
    shanchutc:false,
    member_id:'',
    xinrenyindao1:false,
    xinrenyindao2:false,
    xinrenyindao3:false,
    xinrenyindao4:false,
    weidenglu:false
  },
  z_open(){
    this.setData({
      saixuan:true
    })
  },
  z_close(){
    let navlist2 = [{
      id: "1",
      title: "已收藏",
      xz_flag: false
    },
    {
      id: "2",
      title: "同城",
      xz_flag: false
    },
    {
      id: "3",
      title: "开黑过",
      xz_flag: false
    },
    {
      id: "4",
      title: "可开黑",
      xz_flag: false
    },
    {
      id: "5",
      title: "有主页",
      xz_flag: false
    }
    ]
    this.setData({
      type:0,
      type1:0,
      navlist2:navlist2,
      vip_xz:'',
    })
  },
  z_meizhuye(){
    wx.showToast({
      title: '该会员没有发布主页',
      icon: 'none',
      duration: 2000
    })
  },
  z_wancheng(){
    this.setData({
      saixuan:false
    })
    this.xcx_msg_info2()
  },
  z_sea(e){
    this.setData({
      name:e.detail.value
    })
  },
  z_jiangpin(){
    this.xcx_msg_info2()
  },
  z_shaixuan1(e){
    this.setData({
      type:e.currentTarget.dataset.index + 1
    })
  },
  z_shaixuan2(e){
    this.setData({
      type1:e.currentTarget.dataset.index + 1
    })
  },
  z_shaixuan3: function (e) {
    let _this = this
    let index = e.currentTarget.dataset.index
    let xz_flag = e.currentTarget.dataset.xz_flag
    let id = e.currentTarget.dataset.id
    if (xz_flag) {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: false,
      })
      let arr = _this.data.vip_xz.indexOf(id)
      let vip_xz = _this.data.vip_xz
      vip_xz.splice(arr, 1)
      _this.setData({
        vip_xz: vip_xz
      })
    } else {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: true
      })
      if (_this.data.vip_xz == '') {
        let vip_xz = []
        vip_xz.push(id)
        _this.setData({
          vip_xz: vip_xz
        })
      } else {
        let vip_xz = _this.data.vip_xz
        vip_xz.push(id)
        _this.setData({
          vip_xz: vip_xz
        })
      }
    }
  },
  z_del(e){
    this.setData({
      shanchutc:true,
      member_id:e.currentTarget.dataset.member_id
    })
  },
  z_del1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
      "other_member_id":_this.data.member_id
    }
    wx.request({
      url: app.apiurl + 'api/member/chatMsgListDel',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          shanchutc:false,
          xiaoxi:''
        })
         _this.xcx_msg_info2()
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (wx.getStorageSync("user_token") == "") {
      this.setData({
        weidenglu:true
      })
    } else {
      this.setData({
        weidenglu:false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.guanggao1()
    this.xcx_msg_info1()
    this.xcx_msg_info2()
    // this.xcx_msg_info3()
    this.z_yonghu()
    this.z_3qiyindao()
    this.z_xinrenyindao()
  },
  z_xinrenyindao(){
    if(wx.getStorageSync("xinrenyindao2") == ''){
      this.setData({
        xinrenyindao1:true,
        xinrenyindao2:false,
        xinrenyindao3:false,
      })
    } else if(wx.getStorageSync("xinrenyindao2") == '1'){
      this.setData({
        xinrenyindao1:false,
        xinrenyindao2:true,
        xinrenyindao3:false,
      })
    } else if(wx.getStorageSync("xinrenyindao2") == '2'){
      this.setData({
        xinrenyindao1:false,
        xinrenyindao2:false,
        xinrenyindao3:true,
      })
    } else if(wx.getStorageSync("xinrenyindao2") == '3'){
      this.setData({
        xinrenyindao1:false,
        xinrenyindao2:false,
        xinrenyindao3:false,
      })
    }
  },
  z_xinrenyindao1(){
    wx.setStorageSync("xinrenyindao2", '1')
    this.z_xinrenyindao()
  },
  z_xinrenyindao2(){
    wx.setStorageSync("xinrenyindao2", '2')
    this.z_xinrenyindao()
  },
  z_xinrenyindao3(){
    wx.setStorageSync("xinrenyindao2", '3')
    this.setData({
      xinrenyindao3:false
    })
    this.z_xinrenyindao()
  },
  z_3qiyindao(){
    let _this = this
    if (wx.getStorageSync("3qiyindao") != true) {
      _this.setData({
        tanchuang: true,
      })
    }
  },
  z_close1(){
    wx.setStorageSync("3qiyindao", true)
    this.setData({
      tanchuang: false,
    })
  },
  z_close2(){
    this.setData({
      shanchutc:false
    })
  },
  xcx_msg_info3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/msgInfo?page_no=' + _this.data.page_no + '&page_num=500',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let xiaoxi = _this.data.xiaoxi111
        let xiaoxi1 = []
        for(let i=0;i<res.data.data.list.length;i++){
          if(res.data.data.list[i].state == '0'){
            xiaoxi1.push(i)
          }
        }
        if(xiaoxi1.length > 0){
          xiaoxi = 1
        }
        _this.setData({
          xiaoxi111:xiaoxi
        })
      }
    })
  },
  guanggao1(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        console.log(res.data.data)
         _this.guanggao2()
      }
    })
  },
  guanggao2(){
    let _this = this
    let params = {
      "token":wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=12',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
         _this.setData({
           gglist:res.data.data[0]
         })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
    //我的悬赏消息列表(报名)
    xcx_msg_info1() {
      let _this = this
      let params = {
        "token": wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/member/msgNewInfo',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          // let list = []
          // for(let i=0;i<res.data.data.lenght;i++){
          //   if(res.data.data[i].znx_url == '0'){
          //     list.push('1')
          //   }
          // }
          // if(list.length > 0){
          //   _this.setData({
          //     hongdian:true
          //   })
          // }
          _this.setData({
            list:res.data.data
          })
        }
      })
    },
    xcx_msg_info2() {
      let _this = this
      _this.setData({
        xiaoxi:''
      })
      let order_type = _this.data.type
      if(order_type > 0 && order_type < 7){
        order_type = order_type - 1
      } else if (order_type == 7){
        order_type = 1
      }
      let sex = ''
      if(_this.data.type1 == 1){
        sex = ''
      } else if(_this.data.type1 == 2){
        sex = '男'
      } else if(_this.data.type1 == 3){
        sex = '女'
      }
      let sx_ysc = ''
      if(_this.data.navlist2[0].xz_flag == true){
        sx_ysc = '1'
      }
      let sx_tc = ''
      if(_this.data.navlist2[1].xz_flag == true){
        sx_tc = '1'
      }
      let sx_khg = ''
      if(_this.data.navlist2[2].xz_flag == true){
        sx_khg = '1'
      }
      let sx_kkh = ''
      if(_this.data.navlist2[3].xz_flag == true){
        sx_kkh = '1'
      }
      let sx_hp = ''
      if(_this.data.navlist2[4].xz_flag == true){
        sx_hp = '1'
      }
      let params = {
        "token": wx.getStorageSync("user_token"),
        "page_no":_this.data.page_no,
        "page_num":"10",
        "name":_this.data.name, 
        "order_type": order_type,
      "sex":sex,
      "sx_ysc":sx_ysc,
      "sx_tc":sx_tc,
      "sx_khg":sx_khg,
      "sx_kkh":sx_kkh,
      "sx_hp":sx_hp
      }
      wx.request({
        url: app.apiurl + 'api/member/chatMsgList',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
          _this.setData({
            xiaoxi: res.data.data.list
          })
        }
      })
    },
    z_yonghu(){
      let _this = this
      let params = {
        "token":wx.getStorageSync("user_token"),
      }
      wx.request({
        url: app.apiurl + 'api/member/myInfo',
        method: 'POST',
        data: params,
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 修改请求
        },
        success(res) {
         if(res.data.code == '1001'){
           _this.setData({
             yonghu:res.data.data,
             jg_token:res.data.data.jg_token,
             jg_member_id:res.data.data.jg_member_id
           })
          //  _this.jg_login()
         }
        }
      })
    },
     //即构登录
     jg_login() {
      let _this = this
      let userInfo = {
        userID: "user_" + _this.data.jg_member_id,
        userName: _this.data.yonghu.name
      };
      let token = _this.data.jg_token;
      zim.login(userInfo, token)
        .then(function () {
          console.log("登录成功")
          setTimeout(function(){
            _this.chuangjian()
          },1500)
        })
        .catch(function (err) {
          console.log("登录失败", err)
        });
    },
    chuangjian(){
      var config = {
        // 会话锚点，传空则代表从最新开始查询
        nextConversation: null,
        // 会话一次分页查询数
        count: 20
    };
    
    // 拉取会话列表
    zim.queryConversationList(config)
        .then(function({ conversationList }){
            // 查询成功，开发者需要保存和维护数组内的会话对象
        })
        .catch(function(err){
            // 查询失败
        })
    },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 //下滑刷新
//  let _this = this
//  let page_no  = _this.data.page_no
//  page_no = page_no+1
//  _this.setData({
//    page_no:page_no
//  })
//  _this.xcx_msg_info2()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})